﻿using LD.Host;
using System;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LD.BC
{
    public partial class FrmBcTestPoint : Form
    {
        private TB_BC _bc;
        private DataGridViewRow _SelectRowAxis;
        private DataGridViewRow _SelectRowPoint;
        private ushort _SelectAxisId;
        public FrmBcTestPoint()
        {
            InitializeComponent();
            this.lab_axis.Text = string.Empty;
            this.Shown += FrmBcSettingIO_Shown;
            //BcFactory.MonitorEvent += BcFactory_MonitorEvent;
        }

        private void BcFactory_MonitorEvent(MonitorModel monitor)
        {
            foreach (var item in monitor.AxisState)
            {
                foreach (DataGridViewRow row in this.dgv_data.Rows)
                {
                    if (Convert.ToUInt16(row.Cells["轴Id"].Value) == item.Key)
                    {
                        row.Cells["当前位置"].Value = item.Value.位置;
                        row.Cells["使能"].Value = item.Value.使能;
                        row.Cells["报警"].Value = item.Value.报警;
                        row.Cells["原点"].Value = item.Value.原点;
                        row.Cells["正限位"].Value = item.Value.正限位;
                        row.Cells["负限位"].Value = item.Value.负限位;
                        //row.Cells["急停"].Value = item.Value.;
                        //row.Cells["运动中"].Value = item.Value.;
                        //row.Cells["回零状态"].Value = item.Value.;
                        break;
                    }
                }
            }
        }

        public FrmBcTestPoint(TB_BC bc) : this()
        {
            _bc = bc;
        }
        private void FrmBcSettingIO_Shown(object sender, EventArgs e)
        {
            Init();
        }
        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var dgv = (DataGridView)((sender as ToolStripMenuItem).GetCurrentParent() as ContextMenuStrip).SourceControl;
            dgv.Rows.Add();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var dgv = (DataGridView)((sender as ToolStripMenuItem).GetCurrentParent() as ContextMenuStrip).SourceControl;
            dgv.Rows.Add();
        }
        private void Init()
        {
            this.dgv_point.Rows.Clear();
            this.dgv_data.Rows.Clear();

            var axisParam = _bc.AxisParam.OrderBy(p => p.AxisId).ToList();
            var axisPoint = Appbus.PartNo.Config.AxisPoint.OrderBy(p => p.Sort).ToList();
            foreach (var item in axisParam)
            {
                int index = this.dgv_data.Rows.Add();
                this.dgv_data["轴Id", index].Value = item.AxisId;
                this.dgv_data["轴名称", index].Value = item.Name;
                this.dgv_data["当前位置", index].Value = 0;
                this.dgv_data["使能", index].Value = 0;
                this.dgv_data["报警", index].Value = 0;
                this.dgv_data["原点", index].Value = 0;
                this.dgv_data["正限位", index].Value = 0;
                this.dgv_data["负限位", index].Value = 0;
            }
            foreach (var item in axisPoint)
            {
                int index = this.dgv_point.Rows.Add();
                string axisStr = string.Empty;
                string pointStr = string.Empty;
                //get axis point
                if (item.AxisPointDic != null && item.AxisPointDic.Count > 0)
                {
                    this.dgv_point["point_axis", index].Value = item.ToString();
                }

                this.dgv_point["point_name", index].Value = item.Name;
                this.dgv_point["point_sort", index].Value = item.Sort;
                this.dgv_point["示教", index].Value = "示教";
                this.dgv_point["定位", index].Value = "定位";
            }

            if (axisParam.Count > 0)
            {
                _SelectAxisId = axisParam[0].AxisId;
                this.lab_axis.Text = axisParam[0].Name;
            }
        }
        private void dgv_data_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

        }
        private void btn_goZero_Click(object sender, EventArgs e)
        {
            Task.Run(new Action(() =>
            {
                BcFactory.Instance.Control.Move_BackZero(_SelectAxisId);
            }));
        }

        private void dgv_data_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                _SelectRowAxis = this.dgv_data.SelectedRows[0];
                this.lab_axis.Text = _SelectRowAxis.Cells["轴名称"].Value.ToString();
                _SelectAxisId = Convert.ToUInt16(_SelectRowAxis.Cells["轴Id"].Value);
            }
        }

        private void dgv_point_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;

            if (rowIndex > -1)
            {

                //选中
                _SelectRowPoint = this.dgv_point.SelectedRows[0];
                string pointName = _SelectRowPoint.Cells["point_name"].Value.ToString();
                var point = Appbus.PartNo.Config.AxisPoint.Where(p => p.Name == pointName).FirstOrDefault();
                if (point != null)
                {
                    foreach (DataGridViewRow row in this.dgv_data.Rows)
                    {
                        row.Cells["选中"].Value = false;
                    }
                    foreach (var item in point.AxisPointDic)
                    {
                        foreach (DataGridViewRow row in this.dgv_data.Rows)
                        {
                            if (Convert.ToInt32(row.Cells["轴Id"].Value) == (int)item.Value.AxisId)
                            {
                                row.Cells["选中"].Value = true;
                                break;
                            }
                        }
                    }
                }

                //示教
                if (e.ColumnIndex == 3)
                {
                    if (MessageBox.Show("你确定要示教吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                    {
                        foreach (var item in point.AxisPointDic)
                        {
                            item.Value.Point = BcFactory.Instance.Control.GetAxisPosition(item.Value.AxisId);
                        }
                        this.dgv_point["point_axis", rowIndex].Value = point.ToString();

                        BcFactory.Instance.Save();
                    }
                }
                //移动
                if (e.ColumnIndex == 4)
                {
                    foreach (var item in point.AxisPointDic)
                    {
                        BcFactory.Instance.Control.Move_Location((ushort)item.Value.AxisId, item.Value.Point);
                    }
                }
            }
        }

        private void btn_seven_on_Click(object sender, EventArgs e)
        {
            BcFactory.Instance.Control.SetSevon(_SelectAxisId, true);
        }

        private void btn_seven_off_Click(object sender, EventArgs e)
        {
            BcFactory.Instance.Control.SetSevon(_SelectAxisId, false);
        }

        private void btn_sizeMove__Click(object sender, EventArgs e)
        {
            BcFactory.Instance.Control.Move_Ration(_SelectAxisId, 0, (double)this.num_size.Value);
        }

        private void btn_sizeMove_Click(object sender, EventArgs e)
        {
            BcFactory.Instance.Control.Move_Ration(_SelectAxisId, 1, (double)this.num_size.Value);
        }

        private void btn_reset_Click(object sender, EventArgs e)
        {

        }

        private void btn_stop_Click(object sender, EventArgs e)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(a =>
            {
                BcFactory.Instance.Control.Stop(_SelectAxisId);
            });
        }

        private void FrmBcTestPoint_FormClosing(object sender, FormClosingEventArgs e)
        {
            //BcFactory.MonitorEvent -= BcFactory_MonitorEvent;
        }

        private void btn_jogMove__MouseDown(object sender, MouseEventArgs e)
        {
            BcFactory.Instance.Control.Move(_SelectAxisId, 0);
        }

        private void btn_jogMove_MouseDown(object sender, MouseEventArgs e)
        {
            BcFactory.Instance.Control.Move(_SelectAxisId, 1);
        }

        private void btn_jogMove__MouseUp(object sender, MouseEventArgs e)
        {
            BcFactory.Instance.Control.Stop(_SelectAxisId);
        }

        private void btn_jogMove_MouseUp(object sender, MouseEventArgs e)
        {
            BcFactory.Instance.Control.Stop(_SelectAxisId);
        }

        private void btn_jogMove__Click(object sender, EventArgs e)
        {

        }
    }
}
